package collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortPoint {
    public static void main(String[] args) {
        List<Point> list=new ArrayList<>();
        list.add(new Point(3,5));
        list.add(new Point(20,39));
        list.add(new Point(23,37));
        list.add(new Point(5,29));
        list.add(new Point(10,5));
        list.add(new Point(15,99));
        System.out.println(list);//
        //Collections.sort(list);异常,Point类没有继承系统里comparable()接口
        //无法自然排序,需要自定义排序
        Collections.sort(list, (o1,o2) ->{
                int len1=o1.getA()*o1.getA()+o1.getB()*o1.getB();
                int len2=o2.getA()*o2.getA()+o2.getB()*o2.getB();
                return len1-len2;
            }
        );
        System.out.println(list);

        //
        list.sort((o1,o2) ->{
                int len3=o1.getA()*o1.getA()+o1.getB()*o1.getB();
                int len4=o2.getA()*o2.getA()+o2.getB()*o2.getB();
                return len3-len4;
            }
        );
        System.out.println(list);
    }
}
